/* 
* 用户信息全局类
* commit---用于提交mutatious的同步操作
* dispatch-用户分发actions的异步操作
* 
 */
import {
	ACCESS_TOKEN,
	USER_ID
} from '@/store/mutation-types'
import apps from '@/utils/app.js'
import storage from '@/utils/storage'
import {api_getUserInfo,api_login} from '../../common/api.js'

// 登陆成功后执行
const loginSuccess = (dispatch,commit, { token, id }) => {
  // 过期时间30天
  const expiryTime = 5 * 86400
  // 保存tokne和userId到缓存
  storage.set(USER_ID, id, expiryTime)
  storage.set(ACCESS_TOKEN, token, expiryTime)
  // 记录到store全局变量
  commit('SET_TOKEN', token);
  commit('SET_USER_ID', id);
}

const user = {
	state: {
		// 用户认证token
		token: '',
		// 用户ID
		userId: null,
		// 用户信息
		userInfo:null,
	},
	getters: {
		// 全局配置
		token: state => state.token,
		userId: state => state.userId,
		userInfo: state => state.userInfo,
	},
	mutations: {
		SET_TOKEN: (state, value) => {state.token = value},
		SET_USER_ID: (state, value) => {state.userId = value},
		SET_USER_INFO: (state, value) => {state.userInfo = value}
	},

	actions: {
		login({
			state,
			commit,
			dispatch
		},data){
			uni.showLoading({
				title: "登入中",
				mask: true
			});
			return new Promise((resolve, reject) => {
				api_login(data).then(result => {
					uni.hideLoading();
					const info = result.userinfo
					loginSuccess(dispatch,commit, info);
					commit('SET_USER_INFO',info);
					resolve(result);
				}).catch(err => {
					uni.hideLoading();
					reject(err)
				});
			})
		},
		// 更新用户信息
		updateUserInfo({
			state,
			dispatch,
			commit
		}) {
			return new Promise((resolve, reject) => {
				let params = {
					type:2
				}
				return;
				api_getUserInfo({params}).then(result => {
					uni.hideLoading();
					const info = result.userinfo
					loginSuccess(dispatch,commit, info);
					commit('SET_USER_INFO',info);
					resolve(result);
				}).catch(err => {
					reject(err)
				});
			})
			
		},
		// 退出登录
		Logout({
			commit
		}, data) {
			const store = this
			return new Promise((resolve, reject) => {
				// 删除缓存中的tokne和userId
				storage.remove(USER_ID)
				storage.remove(ACCESS_TOKEN)
				// 记录到store全局变量
				commit('SET_TOKEN', '')
				commit('SET_USER_ID', null)
				commit('SET_USER_INFO', null)
				resolve()
			})
		},
		upload({
			commit,
			dispatch,
			state
		}, data) {
			const store = this
			return new Promise((resolve, reject) => {
				apps.showLoading('上传中..');
				uni.$u.http.upload('addons/installmentloan/wxapp.user/upload', {
					params: {}, /* 会加在url上 */
					filePath: data, // 要上传文件资源的路径。
					name: 'file',
					// #ifdef H5 || APP-PLUS
					timeout: 60000,
					// #endif
					header: {},
					formData: {token:state.token},
					getTask: (task, options) => {
					},
					//validateStatus: (statusCode) => { // statusCode 必存在。此处示例为全局默认配置。演示，非必填选项
					//	return statusCode >= 200 && statusCode < 300
					//}
				}).then(res => {
					uni.hideLoading();
					resolve(res)
					// res.fullurl绝对路径
					// res.url相对路径
					// 返回的res.data 已经进行JSON.parse
				}).catch(err => {
					uni.hideLoading();
					reject(err)
				})
			})
		},
		
	}
}

export default user
